package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.shopping.models.bo.shop.ShopSalesAreaBO;
import com.ztgf.shopping.models.vo.shop.ShopSalesAreaVO;
import com.ztgf.sys.models.dto.SysAreaDTO;
import com.ztgf.sys.models.service.ISysAreaService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopSalesAreaMapper;
import com.ztgf.shopping.models.service.shop.IShopSalesAreaService;
import com.ztgf.shopping.models.bo.shop.ShopSalesAreaSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopSalesAreaDTO;

import java.util.*;

/**
 * 店铺可售区域表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 15:21:49
 */
@DubboService
public class ShopSalesAreaServiceImpl extends ServiceImpl<ShopSalesAreaMapper, ShopSalesAreaDTO> implements IShopSalesAreaService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private ISysAreaService iSysAreaService;

    @Override
    public boolean add(ShopSalesAreaSaveBO saveBO) {
        boolean flag = updateByShopIdAndAreaId(saveBO.getShopId(), saveBO.getAreaId());
        if (flag){
            return false;
        }
        ShopSalesAreaDTO dto = new ShopSalesAreaDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<ShopSalesAreaVO> queryList(ShopSalesAreaBO bo) {
        ArrayList<ShopSalesAreaVO> shopSalesAreaVOS = new ArrayList<ShopSalesAreaVO>();

        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }

        wrapper.orderBy("create_time", false);//降序
        List<ShopSalesAreaDTO> shopSalesAreaDTOS = this.selectList(wrapper);

        for (ShopSalesAreaDTO dto : shopSalesAreaDTOS){
            ShopSalesAreaVO vo = new ShopSalesAreaVO();
            vo.setId(dto.getId());
            vo.setShopId(dto.getShopId());
            vo.setAreaId(dto.getAreaId());
            if (dto.getAreaId() == 0){
                vo.setAreaName("全国");
            }else{
                SysAreaDTO sysAreaDTO = iSysAreaService.selectById(dto.getAreaId());
                vo.setAreaName(sysAreaDTO.getName());
            }

            shopSalesAreaVOS.add(vo);
        }

        return shopSalesAreaVOS;
    }

    @Override
    public boolean updateArea(Long shopId, String ids) {
        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        this.delete(wrapper);

        String [] idA = ids.split(",");
        for (String areaId : idA){
            ShopSalesAreaDTO dto = new ShopSalesAreaDTO();
            dto.setShopId(shopId);
            dto.setAreaId(Long.valueOf(areaId));
            dto.setId(ymlConfig.getPkId());
            this.insert(dto);
        }
        return true;
    }


//    @Override
//    public boolean updateArea(Long shopId, String ids) {
//        String[] ida = ids.split(",");
//        List<String> list= Arrays.asList(ida);
//
//        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
//        wrapper.eq("shop_id", shopId);
//        List<ShopSalesAreaDTO> shopSalesAreaDTOList = this.selectList(wrapper);
//        for (ShopSalesAreaDTO dto : shopSalesAreaDTOList){
//            ShopSalesAreaDTO shopSalesAreaDTO = new ShopSalesAreaDTO();
//            if (list.contains(dto.getId().toString())){
//                shopSalesAreaDTO.setStatus(0);//状态 0使用中，1未使用
//            }else {
//                shopSalesAreaDTO.setStatus(1);//状态 0使用中，1未使用
//            }
//
//            shopSalesAreaDTO.setId(dto.getId());
//            this.updateById(shopSalesAreaDTO);
//        }
//        return true;
//    }

    @Override
    public List<ShopSalesAreaVO> queryAllList(ShopSalesAreaBO bo) {
        ArrayList<ShopSalesAreaVO> shopSalesAreaVOS = new ArrayList<ShopSalesAreaVO>();

        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
        if (bo.getShopId() != null){
            wrapper.eq("shop_id", bo.getShopId());
        }

        wrapper.orderBy("create_time", false);//降序
        List<ShopSalesAreaDTO> shopSalesAreaDTOS = this.selectList(wrapper);

        for (ShopSalesAreaDTO dto : shopSalesAreaDTOS){
            ShopSalesAreaVO vo = new ShopSalesAreaVO();
            vo.setId(dto.getId());
            vo.setShopId(dto.getShopId());
            vo.setAreaId(dto.getAreaId());
            if (dto.getAreaId() == 0){
                vo.setAreaName("全国");
            }else{
                SysAreaDTO sysAreaDTO = iSysAreaService.selectById(dto.getAreaId());
                vo.setAreaName(sysAreaDTO.getName());
            }

            shopSalesAreaVOS.add(vo);
        }

        return shopSalesAreaVOS;
    }

    private boolean updateByShopIdAndAreaId(Long shopId, Long areaId){
        Wrapper<ShopSalesAreaDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        wrapper.eq("area_id", areaId);

        ShopSalesAreaDTO shopSalesAreaDTO = new ShopSalesAreaDTO();
        shopSalesAreaDTO.setShopId(shopId);
        shopSalesAreaDTO.setAreaId(areaId);

        return this.update(shopSalesAreaDTO ,wrapper);
    }
}
